class Solution {
public:
    int lengthOfLastWord(string s) {
        int ans = 0;
        int n = s.size();
        bool f = 0;
        int idx = n - 1;
        for (int i = n - 1; i >= 0; i--) {
            if (s[i] == ' ')idx--;
            else break;
        }
        for (int i = idx; i >= 0; i--) {
            if (s[i] != ' ')ans++;
            else break;
        }
        return ans;
    }
};